// pages/upload-file-vr/uploadFileVR.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fileList: [],
        options: {},
        show: false,
        titleVrList: [],
        fileTitle: "",
        infoId:"",
        currentItem:{
            index:undefined,
            name:undefined
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            options
        })
        this.setData({
            infoId:options.infoId
        })
        this.findVrInfoDetailByInfoId()
        this.onFindVRTitleList()
    },

    changeFiles(e) {
        const files = e.detail;
        this.setData({
            fileList: files
        })
    },
    async findVrInfoDetailByInfoId() {
        let {
            options
        } = this.data
        console.log("----------x"+options.infoId);
        const res = await wx.ajax.get('findVrInfoDetailByInfoId.action', {
            id: options.infoId || ''
        }, true)
        console.log(res.result)
        let files = res.result.map(item => ({
            ...item,
            url: item.file_id,
            name: item.title
        }))
        // const {}
        this.setData({
            fileList: files
        })
    },

    editTile(e) {
        let {index}=e.detail
        this.setData({
            show: true,
            currentItem:{
                ...this.data.currentItem,
                index
            }
        })
    },
    clickItemTitle(e){
        const {name}= e.currentTarget.dataset
        this.setData({
            currentItem:{
                ...this.data.currentItem,
                name
            }
        })
        this.setData({
            fileTitle:name
        })
    },
    onClickTitle(){
        const {name,index}=this.data.currentItem
        console.log(name)
        this.data.fileList[index].name=this.data.fileTitle;
        this.setData({
            show:false,
            fileList:this.data.fileList
        })

    },
    async onFindVRTitleList() {
        let {
            result
        } = await wx.ajax.get('findVrType.action', {
            type: 'cjm'
        })
        // console.log(result);
        this.setData({
            titleVrList: result
        });
    },

    onClose() {
        this.setData({
            show: false
        });
    },

    //保存
    async submitSave() {

        let {
            fileList,
            options
        } = this.data
        if (fileList.length === 0) {
            wx.showToast({
                title: '请上传后在提交',
                icon: 'none'
            })
            return
        }
        let data = {
            title: (this.formatData(fileList, 'name')).toString(),
            info_id: options.infoId || '',
            findVrType: options.findVrType || 1,
            file_id: (this.formatData(fileList, 'url')).toString()
        }
        const res = await wx.ajax.post('addVrInfoDetail.action', data, true)
        wx.showToast({
            title: res.msg,
            icon: 'none'
        })
    },
    formatData(arr, key) {

        return arr.map(item => item[key])


    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})